import Vue from 'vue'
import store from '@/store/index'

// 是否有权限
const hasPermission = userPermission => {
  let userPermissionList = Array.isArray(userPermission) ? userPermission : [userPermission];
  //console.log("=========="+userPermissionList);
  // 当前用户的权限列表
  let permissionList = store.state.perms.permsList;
  if (permissionList == "*") {
    // 所有权限
    return true;
  }
  return userPermissionList.some(e => permissionList.includes(e));
};
// 权限指令
Vue.directive("perms", {
  inserted(el, binding) {
    let perms = store.state.perms.permsList;//按钮权限指令
    //console.log(perms)
    if (perms != '*') {
      if (perms.indexOf(binding.value) < 0) {
        el.parentNode.removeChild(el);//权限不通过删除元素
      } else {
        return false
      }
    } else {
      return false
    }
  }
});
// 全局判断方法
Vue.prototype.$_has = hasPermission;
